package com.tomato.thread.pool;

import java.text.MessageFormat;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CallerRunPolicyTest {
    public static void main(String[] args) {
        final Integer ACPU = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(
                        2 * ACPU + 1,
                        2 * ACPU + 1,
                        0,
                        TimeUnit.SECONDS,
                        new LinkedBlockingDeque<>(6),
                        new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i <= 100; i++) {
            executor.execute(() -> {
                try {
                    // 模拟耗时
                    Thread.sleep(100);
                    System.out.println(MessageFormat.format("{0}执行完成", Thread.currentThread().getName()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
